enum State {
  pending = "pending",
  resolved = "resolved",
  rejected = "rejected",
}

class MyPromiseTS {
  constructor(exclutor) {
    exclutor(this._resove.bind(this), this._reject);
  }

  _state = State.pending;
  _value;

  _resove(res) {
    //   定义promise时 就执行resove()回调
    console.log("this._resove()");
    this._value = res;
    this._state = State.resolved;

    console.log(
      "this._resove() this._onResArry.length",
      this._onResArry.length
    );

    // this._onResArry.forEach((item) => {
    //   item(this._value);
    // });

    // 当promise中定义传入了then的执行回调后
    // while (this._onResArry.length > 0) {
    //   const itemRes = this._onResArry.shift();
    //   itemRes(this._value);
    // }


    let interval = setInterval(() => {
      if (this._onResArry.length > 0) {
        const itemRes = this._onResArry.shift();
        console.log("while{} itemRes()");
        itemRes(this._value);
        clearInterval(interval)
      }
    }, 100)
  }
  _reject(err) {}

  _onResArry = [];

  then(onRes) {
    this._onResArry.push(onRes);
  }
  catch() {}
}

export default MyPromiseTS;

// module.exports = MyPromiseTS